//
//  HOForgetPWDViewContriller.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 1/19/16.
//  Copyright © 2016 ShenOu. All rights reserved.
//

#import "HOForgetPWDViewContriller.h"
#import "HOButton.h"
#import "HOUtils.h"
#import "HOConstants.h"
#import "Logger.h"
#import "HOPWDSettingsViewController.h"
#import "HOWebViewController.h"
#import "HONavigationController.h"
#import "HOUserInfoDataModule.h"
#import "NSString+MD5Addition.h"
#import "UIDevice+IdentifierAddition.h"
#import "HOUserInfoDataModule.h"
#import "HOPWDSettingsViewController.h"
#import "HODefine.h"
#import "HOBindPhoneNumberViewController.h"
@interface HOForgetPWDViewContriller()<UITextFieldDelegate, HOEditTableViewCellDelegate>
{
	int                 secondRecord;
	BOOL                agreedPotocol;
	UIButton            *registerBtn;
}
@end

@implementation HOForgetPWDViewContriller
@synthesize isResetPwd;
@synthesize isBingPhone;
@synthesize settingsViewController;
@synthesize loginViewController;
@synthesize phoneNumber;
- (void)viewDidLoad
{
	[super viewDidLoad];
	[self initTableView];
}


- (void)initViews
{
	[self createBtns];
	baseTableview.scrollEnabled = NO;
}

- (void)createBtns
{
//	CGFloat offset = 35;
//	if(IS_IPHONE_4_SCREEN)
//		offset = 5;
//	if(IS_IPHONE_5_SCREEN)
//		offset = 25;
	registerBtn = [[UIButton alloc]initWithFrame:
				   CGRectMake(HO_HORIZONAL_PADDING,
							  115 + (IS_IPHONE_4_SCREEN ? 0 : 25),
							  self.view.frame.size.width - 2 * HO_HORIZONAL_PADDING,
							  40)];
	[registerBtn.layer setMasksToBounds:YES];
	
	
	[registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
	[registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
	[registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
	[registerBtn setTintColor:[UIColor whiteColor]];
	registerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
	[registerBtn setTitle:NSLocalizedString(@"下一步", @"下一步") forState:UIControlStateNormal];
	[registerBtn addTarget:self action:@selector(goNextStep:) forControlEvents:UIControlEventTouchUpInside];
	[registerBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_NORMAL forView:registerBtn] forState:UIControlStateNormal];
	[registerBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_TOUCHED forView:registerBtn] forState:UIControlStateSelected];
	[registerBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_TOUCHED forView:registerBtn] forState:UIControlStateHighlighted];
	[registerBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_DISABLED forView:registerBtn] forState:UIControlStateDisabled];
	registerBtn.layer.cornerRadius = 3;
	[self.view addSubview:registerBtn];
	
//	if(self.isBingPhone)
	{
		UILabel *lbl1 = [[[UILabel alloc]
						initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
												 registerBtn.frame.size.height + registerBtn.frame.origin.y + 2 + (IS_IPHONE_4_SCREEN ? 0 : 20),
												 registerBtn.frame.size.width,
												 80)] autorelease];
		lbl1.numberOfLines = 0;
		lbl1.font = [UIFont systemFontOfSize:12];
		lbl1.textColor = TEXT_GRAY_COLOR;
		NSString *str = [NSString stringWithFormat:@"%@, %@",  NSLocalizedString(@"若当前号码丢失/不用，或者无法接收到验证码", @"若当前号码丢失/不用，或者无法接收到验证码"), NSLocalizedString(@"欢迎致电客服", @"欢迎致电客服")];
		lbl1.text = str;
		[self.view addSubview:lbl1];
		lbl1.textAlignment = NSTextAlignmentCenter;
		[lbl1 sizeToFit];
		lbl1.frame = CGRectMake(lbl1.frame.origin.x,
								lbl1.frame.origin.y,
								registerBtn.frame.size.width,
								lbl1.frame.size.height);
		
		NSString *content2 = kHOServicePhoneNumber;
		UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
		[button setTitle:content2 forState:UIControlStateNormal];
		[button addTarget:self action:@selector(callPhone) forControlEvents:UIControlEventTouchUpInside];
		button.titleLabel.font = [UIFont systemFontOfSize:12];
		[button setTitleColor:MAIN_COLOR_NORMAL forState:UIControlStateNormal];
		[button setTitleColor:TEXT_GRAY_DARK_COLOR forState:UIControlStateSelected];
		[button setTitleColor:TEXT_GRAY_DARK_COLOR forState:UIControlStateHighlighted];
		[self.view addSubview:button];
//		
//		CGSize size = CGSizeZero;
//		if(IS_LESSTHAN_IOS7)
//		{
//			size = [content2 sizeWithFont:button.titleLabel.font];
//		}
//		else
//		{
//			size = [content2 sizeWithAttributes:@{ NSFontAttributeName:button.titleLabel.font}];
//		}
		
		button.frame = CGRectMake(registerBtn.frame.origin.x,
								  lbl1.frame.origin.y + lbl1.frame.size.height + 5,
								  registerBtn.frame.size.width,
								  22);
	}
	
}

- (void)callPhone
{
	[HOUtils callPhoneNumber:kHOServicePhoneNumber];
}

- (void)viewWillAppear:(BOOL)animated
{
	self.navigationController.navigationBar.hidden = NO;
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	
	[self setNavigationItemTitle:NSLocalizedString(@"忘记密码", @"忘记密码")];
}

- (void)handleTapFrom:(UITapGestureRecognizer *)gesture
{
	[self hideKeyboard];
}

- (void)hideKeyboard
{
	[self.phoneNumberTextField resignFirstResponder];
	[self.verifyCodeTextField resignFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (void)dealloc
{
	[HOUtils removeFromSuperViewForViews:registerBtn];
	[registerBtn release];
	[super dealloc];
}

- (void)goNextStep:(id)sender
{
	if(self.isBingPhone)
	{
		HOBindPhoneNumberViewController *viewCtrl = [[HOBindPhoneNumberViewController alloc]initWithModule:HO_MODULE_TYPE_APP];
		viewCtrl.isBingPhone = isBingPhone;
		viewCtrl.settingsViewController = self.settingsViewController;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	else
	{
		
		
		HOPWDSettingsViewController *viewCtrl = [[HOPWDSettingsViewController alloc] initWithModule:moduleType];
		if(self.isResetPwd)
		{
			viewCtrl.navTitle = NSLocalizedString(@"设置密码", @"设置密码");
		}
		else
		{
			viewCtrl.navTitle = NSLocalizedString(@"重设密码", @"重设密码");
		}
//		viewCtrl.isBelongLoginScreen = self.isBelongLoginScreen;
		viewCtrl.loginViewController = self.loginViewController;
		viewCtrl.isResetPwd = self.isResetPwd;
		viewCtrl.settingsViewController = self.settingsViewController;
		viewCtrl.phoneNumber = self.phoneNumber;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return HO_CELL_HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	
	return 1;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//	if(IS_IPHONE_4_SCREEN)
//		return 10;
//	return 20;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOTableViewCell = @"HOTableViewCell";
	UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTableViewCell];
	if(cell == nil)
	{
		cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOTableViewCell] autorelease];
	}
	
	cell.textLabel.text = NSLocalizedString(@"请联系信息科找回密码", @"请联系信息科找回密码");
	cell.textLabel.textColor = TEXT_COLOR_181818;
	cell.textLabel.font = [UIFont systemFontOfSize:15];
	return cell;
}

#pragma mark
- (void)textFieldDidChange:(NSNotification *)notification
{
	
	HOTextField *textField = (HOTextField *)notification.object;
	NSIndexPath *indexPath = ((HOTextField *)textField).indexPath;
	if(indexPath.row == 0)
	{
		self.phoneNumber = textField.text;
	}
	else if(indexPath.row == 1)
	{
		self.verifyCode = textField.text;
	}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if(![textField isKindOfClass:[HOTextField class]])
		return NO;
	[self goNextStep:nil];
	return YES;
}
#pragma mark



- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	//    LogInf(@"didReceiveResponse response.action:%@ status:%d", response.action, response.baseRequest.status);
	if(![response.action isEqualToString:kHO_ACTION_ResetPassword])
		return;
	
	[super didReceiveResponse:response];
	if([response isRequestDone])
	{
		
	}
	else if(response.baseRequest.status == HO_REQUEST_STATUS_DOING)
	{
//		[self hideKeyboard];
	}
	
}

- (void)didReceiveCommand:(NSString *)action data:(id)data
{
	[super didReceiveCommand:action data:data];
	[super didReceiveCommand:action data:data];
	if([action isEqualToString:kHO_CMD_Got_JPush_RegistrationID])
	{
		[super showLoadingView:NO];
	}
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self.navigationController popToRootViewControllerAnimated:YES];
}
@end

